package com.developmentReformProject.controller;

import com.developmentReformProject.annotation.MyLog;
import com.developmentReformProject.dao.EnterpriseDao;
import com.developmentReformProject.dto.ApplyQueryDto;
import com.developmentReformProject.dto.EnterpriseQueryDto;
import com.developmentReformProject.dto.FiveQueryDto;
import com.developmentReformProject.dto.ThreeQueryDto;
import com.developmentReformProject.entity.Apply;
import com.developmentReformProject.entity.Enterprise;
import com.developmentReformProject.entity.FiveYearProject;
import com.developmentReformProject.entity.ThreeYearProject;
import com.developmentReformProject.service.AuditProjectLibraryService;
import com.developmentReformProject.service.EnterpriseService;
import com.developmentReformProject.service.FiveYearProjectLibraryService;
import com.developmentReformProject.service.ThreeYearProjectLibraryService;
import com.developmentReformProject.utils.PageUtil;
import com.developmentReformProject.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@Slf4j
@RestController
@RequestMapping("/enterprise")
public class EnterpriseController {
    @Autowired
    private EnterpriseService enterpriseService;
    @Autowired
    private EnterpriseDao enterpriseDao;
    @Autowired
    private AuditProjectLibraryService auditProjectLibraryService;
    @Autowired
    private FiveYearProjectLibraryService fiveYearProjectLibraryService;
    @Autowired
    private ThreeYearProjectLibraryService threeYearProjectLibraryService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private HttpSession session;
    @MyLog(value = "企业注册")
    @PostMapping("/register")
    public ResultVo registerEnterprise(@RequestBody Enterprise enterprise){
        enterpriseDao.addEnterprise(enterprise);
        ResultVo resultVo=new ResultVo();
        return resultVo;
    }
    @MyLog(value = "企业修改密码")
    @PostMapping("/updatePassword")
    public ResultVo updatePassword(String enterpriseAccountNumber, String oldPassword, String newPassword) {
        enterpriseService.updatePasswordEnterprise(enterpriseAccountNumber,newPassword,oldPassword);
        ResultVo resultVo=new ResultVo();
        resultVo.setData(enterpriseAccountNumber);
        return  resultVo;
    }
    @MyLog(value = "企业修改自己信息")
    @PostMapping("/updateEnterprise")
    public ResultVo updateEnterprise(@RequestBody Enterprise enterprise){
        enterpriseService.updateEnterprise(enterprise);
        ResultVo resultVo=new ResultVo();
        return resultVo;
    }
    @MyLog(value = "企业登录")
    @PostMapping("/login")
    public ResultVo login(@RequestBody Enterprise enterprise) {
        ResultVo resultVo=new ResultVo();
        if(enterpriseDao.enterpriseLogin(enterprise.getEnterpriseAccountNumber(),enterprise.getEnterprisePassword())==null){
            resultVo=null;
        }else{
            enterprise=enterpriseDao.queryByUsername(enterprise.getEnterpriseAccountNumber());
            resultVo=new ResultVo<>(enterprise);
            request.getSession().setAttribute("enterpriseAccountNumber",enterprise.getEnterpriseAccountNumber());
            request.getSession().setAttribute("enterpriseName",enterprise.getEnterpriseName());
            request.getSession().setAttribute("enterpriseId",enterprise.getEnterpriseId());
            request.getSession().setMaxInactiveInterval(60*60);

        }
        // Cookie cookie=new Cookie("username",admin);
        // response.setHeader("Set-Cookie","JSESSIONID=xxx;SameSite=None;Secure");
        // cookie.setMaxAge(24*60*60);       //存活一天
        //response.addCookie(cookie);

        return resultVo;
    }
    @MyLog(value = "企业查询自己已通过审核项目")
    @GetMapping("/queryEnterpriseProject")
    public ResultVo queryEnterpriseProject(Integer enterpriseId){
        ResultVo resultVo=new ResultVo();
        return resultVo;
    }
    @MyLog(value = "企业查看项目审核进度")
    @GetMapping("/viewProjectAuditProgress")
    public ResultVo viewProjectAuditProgress(Integer enterpriseId){
        enterpriseService.viewProjectAuditProgress(enterpriseId);
        ResultVo resultVo=new ResultVo();
        return resultVo;
    }
    @MyLog(value="企业注销")
    @GetMapping("/logout")
    public String logout(HttpSession session) {
        //清除session
        session.invalidate();
        return "注销成功！";
    }
    @GetMapping("/listDelete")
    public ResultVo listDelete(EnterpriseQueryDto enterpriseQueryDto) throws Exception {
        PageUtil<Enterprise> data =enterpriseService.cleanEnterprise(enterpriseQueryDto);
        ResultVo resultVo=new ResultVo<>(data);
        return  resultVo;
    }
    @PostMapping("/reduction")
    public ResultVo reduction(int enterpriseId) throws Exception{
        Enterprise enterprise=new Enterprise();
        enterprise.setEnterpriseId(enterpriseId);
        enterprise.setEnterpriseStateId(1);
        enterpriseDao.updateEnterprise(enterprise);
        ResultVo resultVo=new ResultVo();
        return resultVo;
    }
    @PostMapping("/cleanAdmin")
    public ResultVo cleanEnterprise(int enterpriseId)throws Exception{
        enterpriseDao.cleanEnterprise(enterpriseId);
        ResultVo resultVo=new ResultVo();
        return resultVo;
    }
    @MyLog(value="test")
    @GetMapping("/apply")
    public ResultVo failApply()throws Exception{
        ApplyQueryDto applyQueryDto=new ApplyQueryDto();
        String enterpriseId = (String) request.getSession().getAttribute("enterpriseId");
        System.out.println(enterpriseId);
       // applyQueryDto.setEnterpriseId(enterpriseId);
       // PageUtil<Apply> data=auditProjectLibraryService.pageList(applyQueryDto);
        ResultVo resultVo=new ResultVo<>();
        return  resultVo;
    }

    @GetMapping("/three")
    public ResultVo three()throws Exception{
        ThreeQueryDto threeQueryDto=new ThreeQueryDto();
        String enterpriseId = (String) request.getSession().getAttribute("enterpriseId");
        //threeQueryDto.setEnterpriseId(enterpriseId);
       // PageUtil<ThreeYearProject> data=threeYearProjectLibraryService.pageList(threeQueryDto);
        ResultVo resultVo=new ResultVo<>();
        return  resultVo;
    }
    @GetMapping("/five")
    public ResultVo five()throws Exception{
        FiveQueryDto fiveQueryDto=new FiveQueryDto();
        String enterpriseId = (String) request.getSession().getAttribute("enterpriseId");
       // fiveQueryDto.setEnterpriseId(enterpriseId);
        //PageUtil<FiveYearProject> data=fiveYearProjectLibraryService.pageList(fiveQueryDto);
        ResultVo resultVo=new ResultVo<>();
        return  resultVo;
    }





}
